package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.hunt.HuntMonster;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class HuntDaoImpl extends TempDao {

    public List<HuntMonster> getHuntMonsterList() {
        ResultSet rs = null;
        CallableStatement call = null;
        List<HuntMonster> dataList = new ArrayList<>();
        String sql = "select * from t_p_hunt_monster order by wave";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                HuntMonster data = new HuntMonster();
                data.setIndex(rs.getInt("wave"));
                data.setBlood(new BigInteger(rs.getString("hp")));
                data.setReward(PropertyHelper.parseStringToProperty(rs.getString("scoreReward")));
                data.setWeightItemList(getWeightItemList(rs.getString("chestPro")));
                String[] paramArr = rs.getString("expReward").split(";");
                data.setPatronsExpMinParam(Integer.parseInt(paramArr[0]));
                data.setPatronsExpMaxParam(Integer.parseInt(paramArr[1]));
                paramArr = rs.getString("moneyReward").split(";");
                data.setSilverMinParam(Integer.parseInt(paramArr[0]));
                data.setSilverMaxParam(Integer.parseInt(paramArr[1]));
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<NormalWeightItem> getWeightItemList(String weightItemParam){
        List<NormalWeightItem> eventList = new ArrayList<>();
        if(StringUtils.isNullOrEmpty(weightItemParam)){
            return eventList;
        }
        String[] itemParamArr = weightItemParam.split("\\|");
        for(String itemParam : itemParamArr){
            String[] param = itemParam.split(";");
            NormalWeightItem weight = new NormalWeightItem();
            weight.setValue(new BigInteger(param[0]));
            weight.setWeight(Integer.parseInt(param[1]));
            eventList.add(weight);
        }
        return eventList;
    }
}
